package com.qqtech.qquan.community.controller;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.enums.CommunityBaseStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityAddrService;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.coupon.service.CouponBaseService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/community/base")
public class CommunityBaseController extends BaseAgentController {
	@Resource
	private CommunityBaseService communityBaseService;
	
	@Resource
	private CommunityAddrService communityAddrService;

	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private CouponBaseService couponBaseService;
	
	@Override
	protected BaseService<CommunityBase> getBaseService() {
		return communityBaseService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/community/communityBase-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CommunityBaseQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		query.setId(super.getCurrCommunityId());//当前后台用户的社区id
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityBaseVo> page = communityBaseService.queryPageList(query, pageKit);
		UserBaseVo userBaseVo = null;
		for (CommunityBaseVo communityBaseVo : page) {
			userBaseVo = userBaseService.getById(communityBaseVo.getCreatorId());
			if (null != userBaseVo) {
				communityBaseVo.setCreatorNick(userBaseVo.getNick());
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		CommunityBaseVo communityBaseVo = communityBaseService.getById(id);
		if(null!=communityBaseVo.getCreatorId()){
			UserBaseVo userBaseVo = userBaseService.getById(communityBaseVo.getCreatorId());
			communityBaseVo.setCreatorNick(userBaseVo.getNick());
		}
		model.addAttribute("communityBaseVo", communityBaseVo);
		if (StringUtil.isNotBlank(communityBaseVo.getImg())) {
			Iterator<String> it = Splitter.on(";").trimResults().omitEmptyStrings().split(communityBaseVo.getPathImgs())
					.iterator();
			List<String> listImgs = Lists.newArrayList(it);
			model.addAttribute("listImgs", listImgs);
		}
		return "/community/communityBase-detail";
	}
	
	/**
	 * 解除屏蔽
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/show")
	@ResponseBody
	public Result show(Integer id) {
		Result ret = new Result();
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setStatus(CommunityBaseStatusDictEnum.NORMAL.getCode());
		communityBase.setIsShow(YesNoEnum.YES.getCode());
		communityBaseService.updateById(communityBase);
		return ret;
	}
	
	/**
	 * 屏蔽
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/hide")
	@ResponseBody
	public Result hide(Integer id) {
		Result ret = new Result();
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setStatus(CommunityBaseStatusDictEnum.INVALID.getCode());
		communityBase.setIsShow(YesNoEnum.NO.getCode());
		communityBaseService.updateById(communityBase);
		return ret;
	}

	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/createSuccess")
	@ResponseBody
	public Result createSuccess(Integer id) {
		Result ret = new Result();
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setStatus(CommunityBaseStatusDictEnum.NORMAL.getCode());
		communityBase.setAuditAt(TimeUtil.now());
		communityBaseService.updateById(communityBase);
		return ret;
	}
	/**
	 * 通过
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/isOpen")
	@ResponseBody
	public Result isOpen(Integer id) {
		Result ret = new Result();
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setIsShow(YesNoEnum.YES.getCode());
		communityBase.setIsOpen(YesNoEnum.YES.getCode());
		communityBaseService.updateById(communityBase);
		return ret;
	}
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/updateImg")
	@ResponseBody
	public Result updateImg(Integer id, String imgPath,String resume,String orderPhone) {
		Result ret = new Result();
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setImg(imgPath);
		communityBase.setResume(resume);
		communityBase.setOrderPhone(orderPhone);
		communityBaseService.updateById(communityBase);
		return ret;
	}
	
	@RequestMapping(value = "/imgUpload", method = RequestMethod.GET)
	public String imgUpload(Model model) {
		return "/community/communityBase-imgUpload";
	}
	
	/**
	 * 提供一个选择社区的页面
	 * @param model
	 * @author Ami
	 * @return
	 */
	@RequestMapping(value = "/choose", method = RequestMethod.GET)
	public String choose(Model model) {
		return "/community/communityBase-choose";
	}
	
	/**
	 * 提供一个选择社区的页面
	 * @author Ami
	 * @return
	 */
	@RequestMapping(value = "/choose", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage choose(CommunityBaseQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityBaseVo> page = communityBaseService.queryPageList(query, pageKit);
		for (CommunityBaseVo communityBaseVo : page) {
			UserBaseVo userBaseVo = userBaseService.getById(communityBaseVo.getCreatorId());
			if (null != userBaseVo) {
				communityBaseVo.setCreatorNick(userBaseVo.getNick());
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	/**
	 * 取消抽奖
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/downLottery")
	@ResponseBody
	public Result downLottery(Integer id) {
		Result ret = new Result();
		CommunityBase communityBase=new CommunityBase();
		communityBase.setIsLottery(YesNoEnum.NO.getCode());
		communityBase.setId(id);
		communityBaseService.updateById(communityBase);
		return ret;
	}
	
	/**
	 * 加入抽奖
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/upLottery")
	@ResponseBody
	public Result upLottery(Integer id) {
		Result ret = new Result();
		CommunityBase communityBase=new CommunityBase();
		communityBase.setIsLottery(YesNoEnum.YES.getCode());
		communityBase.setId(id);
		communityBaseService.updateById(communityBase);
		return ret;
	}
	
	/**
	 * 后台根据cityId、districtId查询社区列表
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "/getListByArea")
	@ResponseBody
	public Result getListByArea(CommunityBaseQuery query) {
		Result ret = new Result();
		query.setIsShow(YesNoEnum.YES.getCode());
		query.setIsOpen(YesNoEnum.YES.getCode());
		Pageable pageKit2 = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		List<CommunityBaseVo> list = communityBaseService.queryList(query, pageKit2);
		ret.put("list", list);
		return ret;
	}
}
